<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_pasien_data extends CI_Model {
	var $table = "pasien";

	public function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$sql = "SELECT * FROM $this->table ORDER BY KdPasien DESC";
		return $sql;
	}

	public function search($keyword){
		$sql = "SELECT * FROM $this->table WHERE Nama like '%$keyword%'";
		return $sql;
	}

	public function store($input){
		$data = array(
			'Nama'	=> $input['nama'],
			'TanggalLahir'	=> iddate2dbdate($input['tanggal']),
			'JKel'	=> $input['jk'],
			'Telp'	=> $input['telp'],
			'Alamat'	=> $input['almt']
			);
		$do_save = $this->db->insert($this->table, $data);
		if($do_save){
			set_flash('success','Data pasien berhasil ditambahkan');
			return true;
		}else{
			set_flash('danger','Data pasien gagal ditambahkan');
			return false;
		}
	}

	public function get_by_id($id){
		$this->db->where('KdPasien', $id);
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($input){
		$data = array(
			'Nama'	=> $input['nama'],
			'TanggalLahir'	=> iddate2dbdate($input['tanggal']),
			'JKel'	=> $input['jk'],
			'Telp'	=> $input['telp'],
			'Alamat'	=> $input['almt']
			);
		$this->db->where('KdPasien', $input['KdPasien']);
		$do_update = $this->db->update($this->table, $data);
		if($do_update){
			set_flash('success','Data pasien berhasil diperbaharui');
			return true;
		}else{
			set_flash('danger','Data pasien gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdPasien', $id);
		$do_delete = $this->db->delete($this->table);

		if($do_delete){
			set_flash('success','Data pasien berhasil dihapus');
			return true;
		}else{
			set_flash('danger','Data pasien gagal dihapus');
			return false;
		}
	}

	public function export(){
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($keyword){
		$where = "Nama LIKE '%$keyword%'";
		$this->db->where($where);

		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}	
}

/* End of file m_pasien_data.php */
/* Location: ./application/models/m_pasien_data.php */